# ScreenHistory - Stack container for screen objects
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

class ScreenHistory:
    """
    ScreenHistory contains latest screens. History is a stack container.
    
    This class is a simple container for Screen objects. It contains all
    recently used screens and allows user to get previous screen from the stack.
    """
    
    def __init__(self, stage, size):
        """
        Initialize screen history.
        @param stage: Stage object
        """
        self.size = size      # How many screens we keep in memory
        self.screens = []     # Screen stack (contains Screen objects)
        self.stage = stage
    
    def __len__(self):
        return len(self.screens)
        
    def add_screen(self, screen):
        """
        Add screen to the history. Push screen into screen stack.
        @param screen: Screen object that is added to the history stack
        """
        if len(self.screens) < self.size:
            self.screens.append(screen)
        else:
            self.stage.remove(self.screens[0].get_group()) # Remove from stage
            del self.screens[0] # Drop the oldest screen from the history
            self.screens.append(screen)
        
    def get_screen(self):
        """
        Get most recent screen from the history. Pops screen from screen stack
        @return Screen object from history stack
        """
        if len(self.screens) == 0:
            return None
        return self.screens.pop()
    
    def is_empty(self):
        """
        Is screen history empty.
        @return boolean value. True if history is empty, otherwise False.
        """
        if len(self.screens) == 0:
            return True
        else:
            return False
        
    def get_number_of_screens(self):
        """
        Get number of screens in history.
        @return integer - Number of screens
        """
        return len(self.screens)
        
    def get_all_screens(self):
        """
        Get all screens from the history. Latest screen is the last one.
        @return List of Screen objects
        """
        return self.screens
